//
//  MessageWebViewController.m
//  ProductB
//
//  Created by lanou on 16/5/31.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "MessageWebViewController.h"

@interface MessageWebViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@property(nonatomic,strong)UIWebView *webView;

@property(nonatomic,strong)UIImageView *iconimage;

@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation MessageWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.iconimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
    [self.iconimage sd_setImageWithURL:[NSURL URLWithString:self.imgString]];
    [self.view addSubview:self.iconimage];
    
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.scrollView.scrollEnabled = YES;
    self.scrollView.delegate = self;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
    label.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:label];
    
    
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, kHeight/3, kWidth, kHeight)];
    self.webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webViewUrl]]];
    self.webView.userInteractionEnabled = NO;
    
    [self.scrollView addSubview:self.webView];
    
    [self.view addSubview:self.scrollView];
}
#pragma mark------------scrollView的代理------------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y<kHeight/3-20 && scrollView.contentOffset.y>0) {
        CGFloat scaleH = scrollView.contentOffset.y/8;
        self.iconimage.frame = CGRectMake(0, 0-scaleH, kWidth, kHeight/3);
    }
    
    if (scrollView.contentOffset.y<0) {
        self.iconimage.transform =
        CGAffineTransformMakeScale(1-(scrollView.contentOffset.y)/100, 1-(scrollView.contentOffset.y)/100);
    }
    if (scrollView.contentOffset.y>=kHeight/3-10) {
        self.webView.userInteractionEnabled = YES;
    }else{
        self.webView.userInteractionEnabled = NO;
    }
}
#pragma mark------------webView代理------------------

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    
    self.scrollView.contentSize = CGSizeMake(0, kHeight/3+kHeight+100);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
